package com.hymjweb.demo.hymj.basic_data_manage.xt_bank.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_bank.bean.XtBankPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface XtBankMapper {

    class XtBankMapperProvider{

        public String findCacheAll() {
            return new SQL() {{
                SELECT("xb.*");
                FROM("xt_bank xb");
                WHERE("1=1");
            }}.toString();
        }


        public String findAll(Map map) {
            String sql= findCacheAll();

            if (map != null) {
                String name = DataTypeUtil.toNotNullString(map.get("keyName"));

                if(!DataTypeUtil.isEmptyStr(name)){
                    sql += " and xb.name like " + DataTypeUtil.quoteAllLikeStr(name);
                }
            }
            return  sql;
        }

        public String findObjectById(String id) {
            return new SQL() {{
                SELECT("xb.*");
                FROM("xt_bank xb");
                WHERE("xb.id = #{id}");
            }}.toString();
        }

        public String insert(XtBankPojo obj) {
            String sql= new SQL() {{
                INSERT_INTO("xt_bank");
                VALUES("id","#{id}");
                VALUES("name","#{name}");
                VALUES("abbreviation","#{abbreviation}");
            }}.toString();
            return sql;
        }

        public String update(XtBankPojo obj) {
            String sql= new SQL() {{
                UPDATE("xt_bank xb");
                SET("xb.name = #{name}");
                SET("xb.abbreviation = #{abbreviation}");
                WHERE("xb.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("xt_bank xb");
                WHERE("xb.id = #{id}");
            }}.toString();
            return sql;
        }

    }

    @Results(id="xtBankMapperId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "abbreviation", property = "abbreviation")
    })
    @SelectProvider(type = XtBankMapperProvider.class, method = "findCacheAll")
    List<XtBankPojo> findCacheAll();

    @ResultMap("xtBankMapperId")
    @SelectProvider(type = XtBankMapperProvider.class, method = "findAll")
    List<XtBankPojo> findAll(Map map);

    @ResultMap("xtBankMapperId")
    @SelectProvider(type = XtBankMapperProvider.class, method = "findObjectById")
    XtBankPojo findObjectById(String id);

    @InsertProvider(type = XtBankMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(XtBankPojo obj);

    @UpdateProvider(type = XtBankMapperProvider.class, method = "update")
    int update(XtBankPojo obj);

    @DeleteProvider(type = XtBankMapperProvider.class, method = "delete")
    int delete(String id);

}
